<template>
  <div class="app-container">

    <!-- 查询表单 -->
    

    <!-- 表格 -->
    <el-table
      :data="list"
      border
      fit
      highlight-current-row>

      <el-table-column
        label="序号"
        width="70"
        align="center">
        <template slot-scope="scope">
          <!-- 表格翻页的序号递增 -->
          {{ (page - 1) * limit + scope.$index + 1 }}
        </template>
      </el-table-column>

      <el-table-column prop="title" label="标题" width="100" />

      <el-table-column label="幻灯片" width="400">
        <template slot-scope="scope">
          <img :src="scope.row.imageUrl" alt="scope.row.title" width="100%" height="100%">
        </template>
      </el-table-column>

      <el-table-column label="创建时间" align="center">
        <template slot-scope="scope">
        {{ scope.row.gmtCreate.substr(0, 10) }}
        </template>
    </el-table-column>
    <el-table-column label="发布时间" align="center">
        <template slot-scope="scope">
        {{ scope.row.gmtModified.substr(0, 10) }}
        </template>
    </el-table-column>

      <el-table-column prop="sort" label="排序" width="60" />

      <el-table-column label="操作" width="200" align="center">
        <template slot-scope="scope">
          <router-link :to="'/cms/banner/edit/'+scope.row.id">
            <el-button type="primary" size="mini" icon="el-icon-edit">修改</el-button>
          </router-link>
          <!-- scope.row 表示当前行 -->
          <el-button type="danger" size="mini" icon="el-icon-delete" @click="removeBannerById(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

   <!-- 分页 -->
    <el-pagination
      background
      :current-page="page"  
      :page-size="limit"
      :total="total"
      style="padding: 30px 0; text-align: center;"
      layout="total,prev, pager, next, jumper"
      @current-change="getPageList">
    </el-pagination>

  </div>
</template>

<script>
import banner from '@/api/cms/banner'

export default {
  data(){
    return{
      list: null, // 数据列表
      total: 0, // 总记录数
      page: 1, // 当前页
      limit: 3 // 每页记录数 
    }
  },
  created(){
    this.getPageList()
  },
  methods:{
    // 获取banner分页列表
    getPageList(page = 1){  // page = 1：表示不传值默认值为1，传值page随之改变
      this.page = page
      banner.getPageBannerList(this.page,this.limit)
        .then(response => {
          this.list = response.data.items
          this.total = response.data.total
          // console.log(this.total)
        })
    },
    
    // 根据id删除讲师
    removeBannerById(id){
      this.$confirm('此操作将永久删除该记录, 是否继续?','提示',{
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
      .then(() => {
        return banner.removeBanner(id)
      })
      .then(() =>{
        this.getPageList()
        this.$message({
          type: 'success',
          message: '删除成功！'
        })
      })
      .catch(response => {
        if (response === 'cancel') {  // cancel：取消按钮的值
            this.$message({
                type: 'info',
                message: '已取消删除'
            })
        } else {
            this.$message({
                type: 'error',
                message: '删除失败！'
            })
        }
      })
    }
  }
}
</script>